/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.neuralstudio;

import org.neuralstudio.utils.Utils;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.table.DefaultTableModel;
import org.jane.utils.AbstractDataset;
import org.jane.utils.SupervisedDataset;
import org.jane.utils.UnsupervisedDataset;
import org.neuralstudio.utils.MyTableModel;

/**
 *
 * @author william
 */
public class TrainingDataManager {

    private static TrainingDataManager instance;
    private HashMap<String,AbstractDataset> datasets;
    private TrainingDataManager() {
        this.datasets = new HashMap<String,AbstractDataset>();
    }
    public static TrainingDataManager getInstance() {
        if (instance == null) createInstance();
        return instance;
    }
    private synchronized static void createInstance() {
         if (instance == null) {
            instance = new TrainingDataManager();
        }
    }
    public void addDataset(String name,AbstractDataset dataset) {
        this.datasets.put(name,dataset);
    }

    public DefaultListModel getListModel() {
        Set<String> datasetNames = this.datasets.keySet();
        DefaultListModel model = new DefaultListModel();
        for (String datasetName : datasetNames) {
            model.addElement(datasetName);
        }
        return model;
    }

    public DefaultComboBoxModel getComboModel(boolean supervised) {
        DefaultComboBoxModel model = new DefaultComboBoxModel();
        Set<Map.Entry<String,AbstractDataset>> mapEntries = this.datasets.entrySet();
        for (Map.Entry<String,AbstractDataset> entry : mapEntries) {
            if (supervised && entry.getValue() instanceof SupervisedDataset) {
                model.addElement(entry.getKey());
            }
            if (!supervised && entry.getValue() instanceof UnsupervisedDataset) {
                model.addElement(entry.getKey());
            }
        }
        return model;
    }
    public AbstractDataset getDataset(String datasetName) {
        return this.datasets.get(datasetName);
    }
    public MyTableModel getTableModel(String datasetName) {
        MyTableModel model = new MyTableModel();
        model.addColumn("Entradas");
        
        try {
            if (this.getDataset(datasetName) instanceof SupervisedDataset) {
                model.addColumn("Salidas");
                for (List<List<Float>> row : ((SupervisedDataset)this.getDataset(datasetName)).getData()) {
                    String[] rowData = new String[2];
                    rowData[0] = Utils.listToCSV(row.get(0));
                    rowData[1] = Utils.listToCSV(row.get(1));
                    model.addRow(rowData);
                }
            }else{
                for (List<Float> row : ((UnsupervisedDataset)this.getDataset(datasetName)).getData()) {
                    String[] rowData = new String[2];
                    rowData[0] = Utils.listToCSV(row);
                    rowData[1] = "";
                    model.addRow(rowData);
                }
            }
            
        }catch(Exception ex){
            return null;
            /*Object[] data = new Object[2];
            data[0] = "0";
            data[1] = "1";
            model.addRow(data);*/
        }
        return model;
    }


}
